﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smart.Data.BaseClasses;
using Smart.Model;
using System.Data.Entity;

namespace Smart.Data.CaloriesCalculator
{
    public class CaloriesCalculatorDL : BaseDataAccess
    {
        private static CaloriesCalculatorDL instance;

        public CaloriesCalculatorDL(ISmartEntities context)
        {
            Context = context;
        }

        public CaloriesCalculatorDL GetInstance(ISmartEntities context = null)
        {
            if (instance == null)
            {
                instance = new CaloriesCalculatorDL(context);
            }
            return instance;
        }

        public List<MealCategory> GetAllCategories()
        {
            return Context.MealCategory.ToList<MealCategory>();
        }
        
        public List<Meal> GetAllMeals()
        {
            return Context.Meal.ToList<Meal>();
        }

        public List<Meal> GetAllMealsFromCategory(int catId)
        {
            return Context.Meal.Where(m => m.CategoryId == catId).ToList<Meal>();
        }

        public Meal GetMealById(int id)
        {
            return Context.Meal.Where(m => m.Id == id).FirstOrDefault();
        }

        public void RecordMeal(MealLog ml)
        {
            if (ml.Id == 0)
            {
                Context.MealLog.AddObject(ml);
            }

            if (Context is SmartEntities)
            {
                (Context as SmartEntities).SaveChanges();
            }
        }

        
    }
}
